/**
 * Tencent is pleased to support the open source community by making MLeaksFinder available.
 *
 * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */

#import "MLeaksMessenger.h"

static __weak UIAlertController *alertView;

@implementation MLeaksMessenger

+ (void)alertWithTitle:(NSString *)title object:(id)object message:(NSString *)message {
    [self alertWithTitle:title message:message object:object additionalButtonTitle:nil];
}

+ (void)alertWithTitle:(NSString *)title
               message:(NSString *)message
                object:(id)object
//              delegate:(id<UIAlertViewDelegate>)delegate
 additionalButtonTitle:(NSString *)additionalButtonTitle {
//    [alertView dismissWithClickedButtonIndex:0 animated:NO];
//    UIAlertView *alertViewTemp = [[UIAlertView alloc] initWithTitle:title
//                                                            message:message
//                                                           delegate:delegate
//                                                  cancelButtonTitle:@"OK"
//                                                  otherButtonTitles:additionalButtonTitle, nil];
//    [alertViewTemp show];
//    alertView = alertViewTemp;
    
    [alertView dismissViewControllerAnimated:YES completion:nil];
    UIAlertController *alert =
    [UIAlertController alertControllerWithTitle:title
                                        message:message
                                 preferredStyle:UIAlertControllerStyleAlert];
    
    //添加默认按钮
    UIAlertAction* destructiveBtn =
    [UIAlertAction actionWithTitle:@"OK"
                             style:UIAlertActionStyleCancel
                           handler:^(UIAlertAction * action) {
        NSLog(@"UIAlertActionStyleCancel");
    }];
    [alert addAction: destructiveBtn];

    if (additionalButtonTitle != nil) {
        UIAlertAction* defaultBtn =
        [UIAlertAction actionWithTitle:additionalButtonTitle
                                 style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
            NSLog(@"UIAlertActionStyleDefault");
            if (!object) {
                return;
            }

        #if _INTERNAL_MLF_RC_ENABLED
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
                [detector addCandidate:object];
                NSSet *retainCycles = [detector findRetainCyclesWithMaxCycleLength:20];
                
                BOOL hasFound = NO;
                for (NSArray *retainCycle in retainCycles) {
                    NSInteger index = 0;
                    for (FBObjectiveCGraphElement *element in retainCycle) {
                        if (element.object == object) {
                            NSArray *shiftedRetainCycle = [self shiftArray:retainCycle toIndex:index];
                            
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [MLeaksMessenger alertWithTitle:@"Retain Cycle"
                                                        message:[NSString stringWithFormat:@"%@", shiftedRetainCycle]];
                            });
                            hasFound = YES;
                            break;
                        }
                        
                        ++index;
                    }
                    if (hasFound) {
                        break;
                    }
                }
                if (!hasFound) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [MLeaksMessenger alertWithTitle:@"Retain Cycle"
                                                message:@"Fail to find a retain cycle"];
                    });
                }
            });
        #endif

        }];
        [alert addAction:defaultBtn];
    }
    NSLog(@"%@: %@ %@", title, message, additionalButtonTitle);
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    if (window.rootViewController != nil) {
        [window.rootViewController presentViewController:alert animated:YES completion:nil];
    }
    alertView = alert;    
}

@end
